package com.zlm.app.partner.controller;

import com.zlm.app.partner.service.UserPartnerService;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.UserConstants;
import com.zlm.common.dto.HxGroupDto;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.entity.UserTokenDO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 合伙人版消息界面(业务与个人版重合，应当考虑从两个服务中拆出，单独成立服务)
 * @Author: duyufu
 * @Date: 2019/7/12 13:49
 */
@RestController
@RequestMapping(value = "/partMessage")
public class MessageController extends CheckController {

    @Autowired
    private UserPartnerService userPartnerService;

    /**
     * 环信合伙人注册
     * @param token
     * @return
     */
    @GetMapping("/huanxinPartRegister")
    public JsonResultDto huanxinPartRegister(String token) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            //环信注册
            JsonResultDto registerResult =  userPartnerService.huanxinRegister(userToken.getUserId());
            checkJsonResultDto(registerResult);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 环信添加好友
     * @param token
     * @param friendHxId
     * @return
     */
    @GetMapping("/huanxinAddFriend")
    public JsonResultDto huanxinAddFriend(String token, String friendHxId) {
        try {
            if (StringUtils.isBlank(friendHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            UserTokenDO userToken = checkToken(token);
            //找了么添加好友
            JsonResultDto result = userPartnerService.addFriend(userToken.getUserId(),friendHxId);
            checkJsonResultDto(result);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 环信删除好友
     * @param token
     * @param friendHxId
     * @return
     */
    @GetMapping("/huanxinDeleteFriend")
    public JsonResultDto huanxinDeleteFriend(String token, String friendHxId) {
        try {
            if (StringUtils.isBlank(friendHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userPartnerService.deleteFriend(userToken.getUserId(),friendHxId);
            checkJsonResultDto(result);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取单个用户
     * @param token
     * @return
     */
    @GetMapping("/huanxinGetUser")
    public JsonResultDto huanxinGetUser(String token,String userHxId) {
        try {
            if (StringUtils.isBlank(userHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            JsonResultDto result = userPartnerService.queryHxUser(userHxId);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取环信好友列表
     * @param token
     * @return
     */
    @GetMapping("/huanxinFriendList")
    public JsonResultDto huanxinFriendList(String token) {
        try {
            UserTokenDO userToken = checkToken(token);
            //找了么好友
            JsonResultDto result = userPartnerService.queryFriendList(userToken.getUserId());
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }


    /**
     * 强制下线
     * @param token
     * @return
     */
    @GetMapping("/huanxinDisconnect")
    public JsonResultDto huanxinDisconnect(String token) {
        try {
            UserTokenDO userToken = checkToken(token);
            userPartnerService.huanxinDisconnect(UserConstants.USER_TYPE_PARTNER_STRING.concat(userToken.getUserId().toString()));
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 搜索好友
     * @param token
     * @param keyword
     * @return
     */
    @GetMapping("/searchFriend")
    public JsonResultDto searchFriend(String token,String keyword) {
        try {
            if (null == keyword) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            JsonResultDto result = userPartnerService.searchFriend(keyword);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取一个用户参与的所有群组
     * @param token
     * @return
     */
    @GetMapping("/getAllJoinedGroup")
    public JsonResultDto getAllJoinedGroup(String token) {
        try {
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userPartnerService.getAllJoinedGroup(userToken.getUserId());
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 创建群组
     * @param group
     * @return
     */
    @PostMapping("/createGroup")
    public JsonResultDto createGroup(HxGroupDto group) {
        try {
            if (null == group.getGroupName()) {
                throw new RuntimeException(CodeConstants.CODE_10020);
            }
            if (null == group.getDescription()) {
                throw new RuntimeException(CodeConstants.CODE_10021);
            }
            checkToken(group.getToken());
            JsonResultDto result = userPartnerService.createGroup(group);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 批量添加群组成员
     * @param group
     * @return
     */
    @PostMapping("/addHxGroupMember")
    public JsonResultDto addHxGroupMember(HxGroupDto group) {
        try {
            if (StringUtils.isBlank(group.getGroupHxId()) || null == group.getMembers() || group.getMembers().size() == 0) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(group.getToken());
            JsonResultDto result = userPartnerService.addHxGroupMember(group);
            checkJsonResultDto(result);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 批量删除群组成员
     * @param group
     * @return
     */
    @PostMapping("/deleteHxGroupMember")
    public JsonResultDto deleteHxGroupMember(HxGroupDto group) {
        try {
            if (StringUtils.isBlank(group.getGroupHxId()) || null == group.getMembers() || group.getMembers().size() == 0) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(group.getToken());
            JsonResultDto result = userPartnerService.deleteHxGroupMember(group);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取群组详情
     * @param token
     * @param groupHxId
     * @return
     */
    @GetMapping("/getGroupDetail")
    public JsonResultDto getGroupDetail(String token,String groupHxId) {
        try {
            if (StringUtils.isBlank(groupHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            JsonResultDto result = userPartnerService.getGroupDetail(groupHxId);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 解散群组
     * @param token
     * @param groupHxId
     * @return
     */
    @GetMapping("/deleteHxGroup")
    public JsonResultDto deleteHxGroup(String token,String groupHxId) {
        try {
            if (StringUtils.isBlank(groupHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userPartnerService.deleteHxGroup(userToken.getUserId(),groupHxId);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 删除单个群组成员
     * @param token
     * @param groupHxId
     * @param userHxId
     * @return
     */
    @GetMapping("/deleteSingleHxGroupMember")
    public JsonResultDto deleteSingleHxGroupMember(String token,String groupHxId,String userHxId) {
        try {
            if (StringUtils.isBlank(userHxId)||StringUtils.isBlank(groupHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            JsonResultDto result = userPartnerService.deleteSingleHxGroupMember(groupHxId,userHxId);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 分页获取群成员
     * @param token
     * @param groupHxId
     * @param pagenum
     * @param pagesize
     * @return
     */
    @GetMapping("/getHxGroupMember")
    public JsonResultDto getHxGroupMember(String token,String groupHxId,Integer pagenum,Integer pagesize) {
        try {
            if (null == pagenum||null == pagesize||StringUtils.isBlank(groupHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            JsonResultDto result = userPartnerService.getHxGroupMember(groupHxId,pagenum,pagesize);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 修改环信群组信息
     * @param token
     * @param groupHxId
     * @param groupname
     * @param description
     * @param maxusers
     * @return
     */
    @GetMapping("/updateHxGroup")
    public JsonResultDto updateHxGroup(String token,String groupHxId,String groupname,String description,Integer maxusers) {
        try {
            if (StringUtils.isBlank(groupHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userPartnerService.updateHxGroup(userToken.getUserId(),groupHxId,groupname,
                    description,maxusers);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 添加群组管理员
     * @param token
     * @param groupHxId
     * @param userHxId
     * @return
     */
    @GetMapping("/addHxGroupAdmin")
    public JsonResultDto addHxGroupAdmin(String token,String groupHxId,String userHxId) {
        try {
            if (StringUtils.isBlank(groupHxId) || StringUtils.isBlank(userHxId) ) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userPartnerService.addHxGroupAdmin(userToken.getUserId(),groupHxId,userHxId);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 移除环信管理员
     * @param token
     * @param groupHxId
     * @param userHxId
     * @return
     */
    @GetMapping("/deleteHxGroupAdmin")
    public JsonResultDto deleteHxGroupAdmin(String token,String groupHxId,String userHxId) {
        try {
            if (StringUtils.isBlank(groupHxId) || StringUtils.isBlank(userHxId) ) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userPartnerService.deleteHxGroupAdmin(userToken.getUserId(),groupHxId,userHxId);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 转让群组
     * @param token
     * @param groupHxId
     * @param userHxId
     * @return
     */
    @GetMapping("/transferHxGroup")
    public JsonResultDto transferHxGroup(String token,String groupHxId,String userHxId) {
        try {
            if (StringUtils.isBlank(groupHxId) || StringUtils.isBlank(userHxId) ) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userPartnerService.transferHxGroup(userToken.getUserId(),groupHxId,userHxId);
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取管理员列表
     * @param token
     * @param groupHxId
     * @return
     */
    @GetMapping("/getHxGroupAdmin")
    public JsonResultDto getHxGroupAdmin(String token,String groupHxId) {
        try {
            if (StringUtils.isBlank(groupHxId)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            checkToken(token);
            JsonResultDto result = userPartnerService.getHxGroupAdmin(groupHxId);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }
}
